import glob
import os
import platform
from cx_Freeze import setup, Executable
import sys
import itertools

def get_excludes(incl):
	def get_file_list(path):
		return [os.path.normpath(p) for p in itertools.chain(glob.iglob(path + ".py"), glob.iglob(path + ".pyd"))]
	def format_module(path):
		return path.replace("\\", ".").replace("/", ".").replace(".py", "").replace(".pyd", "")

	includes = get_file_list("osdep/{0}/*".format(incl))
	return [format_module(p) for p in get_file_list("osdep/*/*") if os.path.isfile(p) and p not in includes]

includefiles = ['readme.txt', 'config.py', 'icon/icon.png', 'plugins/']
includes = []
packages = []
excludes = ['doctest']
icon = 'icon/icon.ico'
target_name = 'TypesettingHelper'

base = None

# make sure only the correct osdependent packages are included
system = platform.system()
if system == "Windows":
	base = "Win32GUI"
	target_name += ".exe"
	excludes += get_excludes("win")
elif system == "Darwin":  # mac os x
	excludes += get_excludes("mac")
	# needed by platform.system() under mac os, not found automatically by cx_freeze
	includes += ["subprocess"]
else:
	print("Illegal platform to install on!")
	sys.exit(1)

setup(
	name='TypesettingHelper',
	version='0.4',
	description='Simple typesetting utility',
	author='voo',
	author_email='voodoon42@gmail.com',
	options={'build_exe': {'excludes': excludes,
						   'includes': includes,
						   'packages': packages,
						   'include_files': includefiles,
						   'force': True
	}},
	executables=[Executable('typesettinghelper.py',
							base=base,
							icon=icon,
							targetName=target_name,
							compress=True)]
)
